//
//  UIButton+Space.m
//  HX2Car
//
//  Created by Mudmen on 2019/12/5.
//  Copyright © 2019 Hx2car_mac. All rights reserved.
//

#import "UIButton+Space.h"

@implementation UIButton (Space)

- (void)setButtonImageWithTitleSpace:(CGFloat)spage locationType:(ButtonLocationType)locationType{
    
    
    CGFloat imageWith = CGRectGetWidth(self.imageView.frame);
    CGFloat titleWith = CGRectGetWidth(self.titleLabel.frame);
    CGFloat imageHeight = CGRectGetHeight(self.imageView.frame);
    CGFloat titleHeight = CGRectGetHeight(self.titleLabel.frame);
//    CGFloat butWith = CGRectGetWidth(self.frame);
    if(locationType == ButtonLocationTypeImageLeftAndTitleRight){
        [self setTitleEdgeInsets:UIEdgeInsetsMake(0, spage, 0, 0)];
        [self setImageEdgeInsets:UIEdgeInsetsMake(0, -spage, 0, 0)];
    }else if (locationType == ButtonLocationTypeImageRightAndTitleLeft){
        [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -imageWith -spage, 0, imageWith)];
        [self setImageEdgeInsets:UIEdgeInsetsMake(0, titleWith, 0, -titleWith - spage)];
    }else if (locationType == ButtonLocationTypeImageUpAndTitleDown){
        [self setTitleEdgeInsets:UIEdgeInsetsMake(imageHeight+spage/2, -imageWith, 0, 0)];

        self.imageEdgeInsets = UIEdgeInsetsMake(0, (self.frame.size.width - imageWith) / 2,  titleHeight + spage, (self.frame.size.width - imageWith) / 2);
       // [self setImageEdgeInsets:UIEdgeInsetsMake(-titleHeight-spage/2, 0,0,-titleWith)];
       // if (titleWith + imageWith > butWith) {
          //   [self setImageEdgeInsets:UIEdgeInsetsMake(-titleHeight-spage/2, 0,0,-self.titleLabel.bounds.size.width)];
      //  }
    }else if (locationType == ButtonLocationTypeImageDownAndTitleUp){
        [self setTitleEdgeInsets:UIEdgeInsetsMake(-imageHeight-spage/2, -imageWith, 0, 0)];
        [self setImageEdgeInsets:UIEdgeInsetsMake(titleHeight+spage/2, 0,0,-titleWith)];
    }
    
}

- (void)setButtonAlignmentLeftWithImageAndTitleSpace:(CGFloat)spage{
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0, spage, 0, 0)];
    [self setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
}

@end
